<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function returnJson($status='', $msg='', $data=[])
{
	$return = [
		'status' => $status,
		'msg' => $msg,
	];

	if(!empty($data))
		$return['data'] = $data;
	
	return json($return); 
}


function encryption_pwd($string)
{
    
    return md5(md5(md5($string)).'boxiang');
    
}

function is_mobile($mobile)
{
	if(!is_numeric($mobile))
	{
		return false;
	}

	if(strlen($mobile) !== 11 || substr($mobile, 0, 1) !== '1')
	{
		return false;
	}


	if(!(preg_match('/^1[34578]\d{9}$/', $mobile)))
	{
		return false;
	}

	return true;
}


function sendMessage($ParamString, $RecNum, $SignName="租介", $TemplateCode="SMS_76385154")
{
	$host = "http://sms.market.alicloudapi.com";
    $path = "/singleSendSms";
    $method = "GET";
    $appcode = "1bf3b3c9756b40a6b3a6d19d9e6ef417";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "ParamString=$ParamString&RecNum=$RecNum&SignName=$SignName&TemplateCode=$TemplateCode";
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }

    curl_exec($curl);
    return true;


    // $ParamString = "{'webname':'','code':'$code','number':'5'}";
    // $RecNum = $phone;
    // $result = sendMessage($ParamString, $RecNum);
}






/**
  * 发送模板短信
  * @param to 手机号码集合,用英文逗号分开
  * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
  * @param $tempId 模板Id
  */       
function sendTemplateSMS($to,$datas,$tempId)
{
	include_once("/CCPRestSDK.php");

	//主帐号
	$accountSid= '8a216da85b602cda015b8a7748161263';

	//主帐号Token
	$accountToken= '8ee9001fef8146b99ae701c8f9b14d7c';

	//应用Id
	$appId='8a216da85b602cda015b8a77489c1268';

	//请求地址，格式如下，不需要写https://
	$serverIP='app.cloopen.com';

	//请求端口 
	$serverPort='8883';

	//REST版本号
	$softVersion='2013-12-26';
     // 初始化REST SDK
    global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
    $rest = new REST($serverIP,$serverPort,$softVersion);
    $rest->setAccount($accountSid,$accountToken);
    $rest->setAppId($appId);
    
     // 发送模板短信
     // echo "Sending TemplateSMS to $to <br/>";
    $result = $rest->sendTemplateSMS($to,$datas,$tempId);
    if($result == NULL ) {
     	return returnJson(3, 'result error!');
    }
    if($result->statusCode!= 0) {
     	return returnJSon($result->statusCode, $result->statusMsg);
         //TODO 添加错误处理逻辑
    }else
    {
        return returnJSon(0, '短信发送成功！');
    }
}

/**
 * @todo 获取客户端ip地址
 */
function getIP() {
    $ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])){
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
        for ($i = 0; $i < count($ips); $i++) {
            if (!preg_match ("/^(10│172.16│192.168)$/", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}



